﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JUtil;

namespace DalGenerator
{
    public partial class DalGeneratorForm : Form
    {
        private DalGeneratorConfig CurConfig
        {
            get
            {
                return new DalGeneratorConfig
                {
                    Namespace = Namespace,
                    ConnectionString = ConnectionString,
                    DbProvider = DbProvider,
                    Class = Class,
                    Sql = Sql,
                    ExportDirectory = ExportDirectory,
                    ExploreAfterRun = ExploreAfterRun
                };
            }
        }

        private string Namespace
        {
            get
            {
                return txtNAMESPACE.Text;
            }
            set
            {
                txtNAMESPACE.Text = value;
            }
        }
        private string ConnectionString
        {
            get
            {
                return txtConnectionString.Text;
            }
            set
            {
                txtConnectionString.Text = value;
            }
        }

        private class ListControlItem
        {
            public object Value { get; set; }
            public string Text { get; set; }
        }

        private DbProvider DbProvider
        {
            get
            {
                ListControlItem item = (ListControlItem)cbxDbProvider.SelectedItem;
                return (DbProvider)item.Value;
            }
            set
            {
                ComboBox cb = cbxDbProvider;

                int iDefaultValue = Convert.ToInt32(value);
                for (int i = 0; i < cb.Items.Count; i++)
                {
                    ListControlItem item = (ListControlItem)cb.Items[i];

                    int iValue = Convert.ToInt32(item.Value);

                    if ((iDefaultValue & iValue) == iValue)
                        cb.SelectedIndex = i;
                }
            }
        }

        private bool isByTable
        {
            get
            {
                return rbByTable.Checked;
            }
            set
            {
                rbByTable.Checked = value;
            }
        }

        private string Class
        {
            get
            {
                if (isByTable)
                {
                    CheckedListBox clb = cklstbxTables;

                    foreach (string item in clb.CheckedItems)
                        return item;

                    return string.Empty;
                }
                else
                {
                    return txtClass.Text;
                }
            }
            set
            {
                if (isByTable)
                {
                    CheckedListBox cklstbx = cklstbxTables;

                    for (int i = 0; i < cklstbx.Items.Count; i++)
                    {
                        string item = (string)cklstbx.Items[i];

                        bool contain = item.Contains(value);

                        CheckState state = (contain) ? CheckState.Checked : CheckState.Unchecked;

                        cklstbx.SetItemCheckState(i, state);
                    }
                }
                else
                {
                    txtClass.Text = value;
                }
            }
        }

        private string Sql
        {
            get
            {
                if (isByTable)
                {
                    return string.Format("select * from {0}", Class);
                }
                else
                {
                    return txtSql.Text;
                }
            }
            set
            {
                if (isByTable)
                {
                    string strClass = value;
                    strClass = strClass.Replace("select", "");
                    strClass = strClass.Replace("*", "");
                    strClass = strClass.Replace("from", "");
                    strClass = strClass.Trim();
                    Class = strClass;
                }
                else
                {
                    txtSql.Text = value;
                }
            }
        }

        private string ExportDirectory
        {
            get
            {
                return txtExportDirectory.Text;
            }
        }
        private bool ExploreAfterRun
        {
            get
            {
                return chkExploreAfterRun.Checked;
            }
        }

        public DalGeneratorForm()
        {
            InitializeComponent();

            rbByTable.CheckedChanged += (sender, e) => { UpdatePaenlContainer(); };
            rbBySql.CheckedChanged += (sender, e) => { UpdatePaenlContainer(); };

            InitComboBox<DbProvider>(cbxDbProvider);

            bool byTable = true;
            if (byTable)
                rbByTable.Checked = true;
            else
                rbBySql.Checked = true;

        }

        private void UpdatePaenlContainer()
        {
            pnlByTable.Visible = isByTable;
            pnlBySql.Visible = !isByTable;
        }

        /// <summary>
        /// 重設匯出檔案路徑
        /// </summary>
        private void btnResetExportDirectory_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();

            dlg.Description = "請選擇匯出資料夾";

            dlg.ShowNewFolderButton = true;

            string ExportDirectory = txtExportDirectory.Text;
            if (JUtil.Path.Directory.Exists(ExportDirectory))
                dlg.SelectedPath = ExportDirectory;

            if (dlg.ShowDialog() == DialogResult.OK)
                txtExportDirectory.Text = dlg.SelectedPath;
        }

        /// <summary>
        /// 針對列舉行別初始化ComboBox
        /// </summary>
        private void InitComboBox<T>(ComboBox cbx) where T : struct, IConvertible
        {
            cbx.Items.Clear();

            Type t = typeof(T);

            foreach (T Value in Enum.GetValues(t))
            {
                string Text = Enum.GetName(t, Value);

                cbx.Items.Add(new ListControlItem { Text = Text, Value = Value });
            }

            cbx.DisplayMember = "Text";
            cbx.ValueMember = "Value";
        }

        /// <summary>
        /// 重新載入資料表
        /// </summary>
        private void btnRefresh_Click(object sender, EventArgs e)
        {
        }

        private void btnRun_Click(object sender, EventArgs e)
        {

        }
    }

    public class DalGeneratorConfig
    {
        public string Namespace { get; set; }
        public string ConnectionString { get; set; }
        public DbProvider DbProvider { get; set; }
        public string Class { get; set; }
        public string Sql { get; set; }
        public string ExportDirectory { get; set; }
        public bool ExploreAfterRun { get; set; }
    }

}
